//
//  SQNoticeListViewController.m
//  pai
//
//  Created by linjunjing on 14-11-19.
//  Copyright (c) 2014年 linjunjing. All rights reserved.
//

#import "SQNoticeListViewController.h"
#import "SQNoticeContentViewController.h"


@interface SQNoticeListViewController ()

@end

@implementation SQNoticeListViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setTitle:@"通知中心"];
    
    [self showList];
}

-(void)getOrderList
{
    //[self showList];
    pull_unread_note_t note;
    note.base.msgtype = 1012;
    note.base.len = sizeof(note);
    note.base.error = 0;
    strcpy(note.username, [[Global instance].userinfo.username UTF8String]);
    
    NSMutableData* msgData = [NSMutableData data];
    [msgData appendBytes:&note length:sizeof(note)];
    
    [super sendData:msgData tag:1 withHUD:YES];
}

-(void)showList
{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(303, 55);
    flowLayout.sectionInset = UIEdgeInsetsMake(9, 0, 0, 0);
    
    self.myCollection = [[UICollectionView alloc]initWithFrame:CGRectMake(0, self.MyNavigationBar.frame.origin.y+self.MyNavigationBar.frame.size.height, 320, [[UIScreen  mainScreen] bounds].size.height - 44 - self.MyNavigationBar.frame.origin.y-self.MyNavigationBar.frame.size.height) collectionViewLayout:flowLayout];
    self.myCollection.backgroundColor = [UIColor colorWithRed:240/255.0 green:240/255.0 blue:240/255.0 alpha:1];
    [self.myCollection registerClass:[SQNoticeListViewCell class] forCellWithReuseIdentifier:@"SQNoticeListViewCell"];
    [self.view addSubview:self.myCollection];
    
    self.myCollection.delegate = self;
    self.myCollection.dataSource = self;
    
    [self getOrderList];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark - Collection View Data Source
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataMArr.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"show");
    static NSString *cellIdentifier = @"SQNoticeListViewCell";
    SQNoticeListViewCell *cell = (SQNoticeListViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
    sqlTestList* list = (sqlTestList*)self.dataMArr[indexPath.row];
    [cell setDic:list];
    return cell;
};
//设置元素的的大小框
//-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
//{
//    UIEdgeInsets top = {0,10,0,20};
//    return top;
//}
//设置顶部的大小
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    CGSize size={9,20};
    return size;
}
//设置元素大小
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake(303,65);
}
//点击元素触发事件
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"low auction view controller:%@",indexPath);
    SQNoticeContentViewController* ncvc = [[SQNoticeContentViewController alloc]init];
    ncvc.list = [self.dataMArr objectAtIndex:indexPath.row];
    [self.navigationController pushViewController:ncvc animated:YES];
}
//读取数据
-(void)onSocket:(AsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag
{
    if (tag == 1) {
        if (self.socketLength == 0) {
            ack_pull_unread_note_t baseInfo;
            [data getBytes:&baseInfo length:sizeof(baseInfo)];
            NSLog(@"%d",baseInfo.base.msgtype);
            NSLog(@"%d",baseInfo.base.len);
            NSLog(@"%d",baseInfo.base.error);
            self.templength =  data.length;
            self.socketLength = baseInfo.base.len;
        }
        else{
            self.templength += data.length;
        }
        if (self.socketTempData == nil) {
            self.socketTempData = [NSMutableData dataWithData:data];
        }else{
            [self.socketTempData appendData:data];
        }
        if (self.templength < self.socketLength) {
            [asyncSocket readDataWithTimeout:-1 tag:tag];
            return;
        }else{
            ack_pull_unread_note_t baseInfo;
            [self.socketTempData getBytes:&baseInfo length:sizeof(ack_pull_unread_note_t)];
            NSString* body = [NSString stringWithUTF8String: baseInfo.body];
            if (body.length > 1) {
                //body有数据
                //分割 插入数据
                [self insetDB:body];
            }
            self.socketTempData = nil;
            self.socketLength = 0;
            self.templength = 0;
            [self.HUD showAnimated:YES whileExecutingBlock:^{
                [self.HUD show:NO];
            } completionBlock:^{
                //读取数据并显示
            }];
            //加载
            //[self.lowTableView headerEndRefreshing];
            //更新列表
            //[self.lowTableView reloadData];
            [self selectDB];
        }
    }
}
-(void)insetDB:(NSString *)str
{
    NSArray* arr = [str componentsSeparatedByString:@"$"];
    NSLog(@"%ld",(long)arr.count);
    for (NSInteger i=0; i<arr.count; i++) {
        NSString* tempstr = [arr objectAtIndex:i];
        NSLog(@"%@",tempstr);
        NSArray* temparr = [tempstr componentsSeparatedByString:@"#"];
        NSLog(@"%ld",(long)temparr.count);
        sqlTestList* list = [[sqlTestList alloc] init];
        list.noticeTitle = [temparr objectAtIndex:0];
        list.noticeContent = [temparr objectAtIndex:1];
        list.username = [Global instance].userinfo.username;
        list.noticeType = 1;
        _noticeDB = [[SQMySQL alloc]init];
        [_noticeDB insertTestList:list];
    }
}
-(void)selectDB
{
    NSLog(@"selectDB");
    _noticeDB = [[SQMySQL alloc]init];
    sqlTestList* list = [[sqlTestList alloc] init];
    list.username = [Global instance].userinfo.username;
    self.dataMArr = nil;
    self.dataMArr = [NSMutableArray array];
    self.dataMArr = [_noticeDB getTestList:list];
    [self.myCollection reloadData];
}
@end
